package com.hzqy.service.zxbh;

import com.hzqy.commons.utils.HttpUtil;
import com.hzqy.commons.utils.SystemConfigUtils;
import com.hzqy.persistence.dao.zxbh.ZxbhUserDao;
import com.hzqy.web.vo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ZxbhUserService {
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private ZxbhUserDao zxbhUserDao;
	
	public int addUserCredit(UserCreditVo userCreditVo){
		int f_uaz_id=userCreditVo.getF_uc_userId();
		UserAccountVo userAccountVo = zxbhUserDao.selectUserAccount(f_uaz_id);
		BigDecimal yue = null;
		if(userAccountVo.getF_uaz_sumUserCredit()==null){
			yue = userCreditVo.getF_uc_fee();
		}else{
			yue = userAccountVo.getF_uaz_sumUserCredit().add(userCreditVo.getF_uc_fee());
		}
		userCreditVo.setF_uc_balance(yue);
		userAccountVo.setF_uaz_sumUserCredit(yue);
		zxbhUserDao.updateUserAccount(userAccountVo);
		int flag = zxbhUserDao.addUserCredit(userCreditVo);
		return flag;
	}
	
	public Map<String,Object> selectUserCreditList(UserCreditVo userCreditVo, int curPage, int pageSize){
		Map<String,Object> map = new HashMap<String, Object>();
		Integer rowCount = zxbhUserDao.findUserCreditCount(userCreditVo);
		map.put("total", rowCount);
		if(rowCount > 0 && curPage > 0 && pageSize > 0) {
			map.put("firstResult", (curPage-1)*pageSize);
			map.put("maxResults", pageSize);
			map.put("f_uaz_realName", userCreditVo.getF_uaz_realName());
			map.put("f_uc_status", userCreditVo.getF_uc_status());
			map.put("f_uc_source", userCreditVo.getF_uc_source());
			map.put("f_po_ids", userCreditVo.getF_po_ids());
			List<UserCreditVo> results = zxbhUserDao.selectUserCreditList(map);
			map.put("rows", results);
		} else {
			map.put("rows", new ArrayList<UserCreditVo>());
		}
		return map;
	}
	
	public Map<String,Object> selectUserAccountList(UserAccountVo userAccountVo, int curPage, int pageSize){
		Map<String,Object> map = new HashMap<String, Object>();
		Integer rowCount = zxbhUserDao.findUserAccountCount(userAccountVo);
		map.put("total", rowCount);
		if(rowCount > 0 && curPage > 0 && pageSize > 0) {
			map.put("firstResult", (curPage-1)*pageSize);
			map.put("maxResults", pageSize);
			map.put("f_uaz_realName", userAccountVo.getF_uaz_realName());
			map.put("f_uaz_phone", userAccountVo.getF_uaz_phone());
			map.put("f_uaz_status", userAccountVo.getF_uaz_status());
			map.put("f_uaz_source", userAccountVo.getF_uaz_source());
			map.put("f_uaz_name", userAccountVo.getF_uaz_name());
			map.put("f_uaz_sn", userAccountVo.getF_uaz_sn());
			map.put("f_uaz_orgId", userAccountVo.getF_uaz_orgId());
			map.put("f_po_ids", userAccountVo.getF_po_ids());
			map.put("f_uazw_phone", userAccountVo.getF_uazw_phone());
			map.put("f_uazw_openId", userAccountVo.getF_uazw_openId());
			List<UserAccountVo> results = zxbhUserDao.selectUserAccountList(map);
			map.put("rows", results);
		} else {
			map.put("rows", new ArrayList<UserAccountVo>());
		}
		return map;
	}
	
	public int addUserAccount(UserAccountVo userAccountVo){
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("f_uaz_sn", userAccountVo.getF_uaz_sn());
		map.put("firstResult", 0);
		map.put("maxResults", 1);
		List<UserAccountVo> results = zxbhUserDao.selectUserAccountList(map);
		int flag = 0;
		if(results.isEmpty()){
			flag = zxbhUserDao.addUserAccount(userAccountVo);
		}else{//sn码重复
			flag = -2;
		}
		return flag;
	}
	
	public int deleteUserAccount(int f_uaz_id){
		int flag = zxbhUserDao.deleteUserAccount(f_uaz_id);
		return flag;
	}
	
	public int updateUserAccount(UserAccountVo userAccountVo){
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("f_uaz_sn", userAccountVo.getF_uaz_sn());
		map.put("firstResult", 0);
		map.put("maxResults", 1);
		List<UserAccountVo> results = zxbhUserDao.selectUserAccountList(map);
		int flag = 0;
		if(!results.isEmpty()&&results.get(0).getF_uaz_id()!=userAccountVo.getF_uaz_id()){//sn码重复
			flag = -2;
		}else{
			flag = zxbhUserDao.updateUserAccount(userAccountVo);
		}
		return flag;
	}
	
	public UserAccountVo selectUserAccount(int f_uaz_id){
		UserAccountVo userAccountVo = zxbhUserDao.selectUserAccount(f_uaz_id);
		return userAccountVo;
	}
	
	public Map<String,Object> selectUserAccountWchatList(UserAccountZxbhWchatVo userAccountZxbhWchatVo, int curPage, int pageSize){
		Map<String,Object> map = new HashMap<String, Object>();
		Integer rowCount = zxbhUserDao.findAccountZxbhWchatCount(userAccountZxbhWchatVo);
		map.put("total", rowCount);
		if(rowCount > 0 && curPage > 0 && pageSize > 0) {
			map.put("firstResult", (curPage-1)*pageSize);
			map.put("maxResults", pageSize);
			map.put("f_uazw_userId", userAccountZxbhWchatVo.getF_uazw_userId());
			List<UserAccountZxbhWchatVo> results = zxbhUserDao.selectUserAccountWchatList(map);
			map.put("rows", results);
		} else {
			map.put("rows", new ArrayList<UserAccountZxbhWchatVo>());
		}
		return map;
	}
	
	public int addUserAccountWchat(UserAccountZxbhWchatVo userAccountZxbhWchatVo){
		UserAccountVo userAccountVo = zxbhUserDao.selectUserAccount(userAccountZxbhWchatVo.getF_uazw_userId());
		userAccountZxbhWchatVo.setF_uazw_sn(userAccountVo.getF_uaz_sn());
		int flag = zxbhUserDao.addUserAccountWchat(userAccountZxbhWchatVo);
		return flag;
	}
	
	public int deleteUserAccountWchat(int f_uazw_id){
		int flag = zxbhUserDao.deleteUserAccountWchat(f_uazw_id);
		return flag;
	}
	
	public int updateUserAccountWchat(UserAccountZxbhWchatVo userAccountZxbhWchatVo){
		int flag = zxbhUserDao.updateUserAccountWchat(userAccountZxbhWchatVo);
		return flag;
	}
	
	public UserAccountZxbhWchatVo selectUserAccountWchat(int f_uazw_id){
		UserAccountZxbhWchatVo userAccountZxbhWchatVo = zxbhUserDao.selectUserAccountWchat(f_uazw_id);
		return userAccountZxbhWchatVo;
	}
	
	public Map<String, Object> clearUserBySn(String sn){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String web_ips= SystemConfigUtils.getSysConfig("web_ips");
		if(web_ips.indexOf(",")>-1){
			String[] ipArr = web_ips.split(",");
			for(int i=0;i<ipArr.length;i++){
				String url = "http://" + ipArr[i] + "/web/zxbh/refreshUserBySn/"+sn;
				LOG.debug("清除前台用户缓存sn："+sn+",前台地址为："+url);
				Map<String, String> map = HttpUtil.get(url,3000,3000);
				LOG.debug("清除前台用户缓存sn："+sn+",前台地址为："+url+",返回信息为："+map);
			}
		}else{
			String url = "http://" + web_ips + "/web/zxbh/refreshUserBySn/"+sn;
			LOG.debug("清除前台用户缓存sn："+sn+",前台地址为："+url);
			Map<String, String> map = HttpUtil.get(url,3000,3000);
			LOG.debug("清除前台用户缓存sn："+sn+",前台地址为："+url+",返回信息为："+map);
		}
		resultMap.put("flag", 1);
		resultMap.put("msg", "清除成功");
		return resultMap;
	}
	
	public Map<String, Object> clearWebCache(){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String web_ips= SystemConfigUtils.getSysConfig("web_ips");
		if(web_ips.indexOf(",")>-1){
			String[] ipArr = web_ips.split(",");
			for(int i=0;i<ipArr.length;i++){
				String url = "http://" + ipArr[i] + "/web/clear_cache";
				LOG.debug("清除前台缓存,前台地址为："+url);
				Map<String, String> map = HttpUtil.get(url,3000,3000);
				LOG.debug("清除前台缓存,前台地址为："+url+",返回信息为："+map);
			}
		}else{
			String url = "http://" + web_ips + "/web/clear_cache";
			LOG.debug("清除前台缓存,前台地址为："+url);
			Map<String, String> map = HttpUtil.get(url,3000,3000);
			LOG.debug("清除前台缓存,前台地址为："+url+",返回信息为："+map);
		}
		resultMap.put("flag", 1);
		resultMap.put("msg", "清除成功");
		return resultMap;
	}

	public Map<String,Object> selectApkUpgradeList(ApkUpgradeVo apkUpgradeVo, int curPage, int pageSize){
		Map<String,Object> map = new HashMap<String, Object>();
		Integer rowCount = zxbhUserDao.findApkUpgradeCount(apkUpgradeVo);
		map.put("total", rowCount);
		if(rowCount > 0 && curPage > 0 && pageSize > 0) {
			map.put("firstResult", (curPage-1)*pageSize);
			map.put("maxResults", pageSize);
			map.put("f_au_name", apkUpgradeVo.getF_au_name());
			map.put("f_au_upgrade_type", apkUpgradeVo.getF_au_upgrade_type());
			map.put("f_au_status", apkUpgradeVo.getF_au_status());
			List<ApkUpgradeVo> results = zxbhUserDao.selectApkUpgradeList(map);
			map.put("rows", results);
		} else {
			map.put("rows", new ArrayList<ApkUpgradeVo>());
		}
		return map;
	}

	public Map<String,Object> addApkUpgrade(ApkUpgradeVo apkUpgradeVo){
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = 0;
		int f_au_version_code = apkUpgradeVo.getF_au_version_code();
		if(f_au_version_code==1){
			flag = -1;
		}else{
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("firstResult", 0);
			map.put("maxResults", 1);
			map.put("f_au_upgrade_type", apkUpgradeVo.getF_au_upgrade_type());
			map.put("f_au_status", apkUpgradeVo.getF_au_status());
			map.put("order_field", "f_au_version_code");
			List<ApkUpgradeVo> results = zxbhUserDao.selectApkUpgradeList(map);
			if(results.isEmpty()){
				flag = zxbhUserDao.addApkUpgrade(apkUpgradeVo);
			}else if(results.get(0).getF_au_version_code()<f_au_version_code){//f_au_version_code递增，可以入库
				flag = zxbhUserDao.addApkUpgrade(apkUpgradeVo);
			}else{
				resultMap.put("result","新增失败!versionCode需要大于"+results.get(0).getF_au_version_code()+"！");
				flag = -2;//不可入库
			}
		}
		if(flag>0) {
			resultMap.put("success", true);
			resultMap.put("result","新增成功!");
		} else if(flag == -1){
			resultMap.put("success", false);
			resultMap.put("result","新增失败!versionCode不能填1！");
		} else if(flag == -2){
			resultMap.put("success", false);
		} else {
			resultMap.put("success", false);
			resultMap.put("result","新增失败!flag="+flag);
		}
		return resultMap;
	}

	public int updateApkUpgrade(ApkUpgradeVo apkUpgradeVo){
		int flag = zxbhUserDao.updateApkUpgrade(apkUpgradeVo);
		return flag;
	}

	public Map<String,Object> selectZlxUrlList(StatZxbhConfUrlVo statZxbhConfUrlVo, int curPage, int pageSize){
		Map<String,Object> map = new HashMap<String, Object>();
		Integer rowCount = zxbhUserDao.findZlxUrlCount(statZxbhConfUrlVo);
		map.put("total", rowCount);
		if(rowCount > 0 && curPage > 0 && pageSize > 0) {
			map.put("firstResult", (curPage-1)*pageSize);
			map.put("maxResults", pageSize);
			map.put("f_szcu_url", statZxbhConfUrlVo.getF_szcu_url());
			map.put("f_szcu_name", statZxbhConfUrlVo.getF_szcu_name());
			map.put("f_szcu_type", statZxbhConfUrlVo.getF_szcu_type());
			map.put("f_szcu_status", statZxbhConfUrlVo.getF_szcu_status());
			List<StatZxbhConfUrlVo> results = zxbhUserDao.selectZlxUrlList(map);
			map.put("rows", results);
		} else {
			map.put("rows", new ArrayList<StatZxbhConfUrlVo>());
		}
		return map;
	}

	public StatZxbhConfUrlVo selectzlxUrl(int f_szcu_id){
		StatZxbhConfUrlVo statZxbhConfUrlVo = zxbhUserDao.selectzlxUrl(f_szcu_id);
		return statZxbhConfUrlVo;
	}

	public int addZlxUrl(StatZxbhConfUrlVo statZxbhConfUrlVo){
		int flag = zxbhUserDao.addZlxUrl(statZxbhConfUrlVo);
		return flag;
	}

	public int deleteZlxUrl(int f_szcu_id){
		int flag = zxbhUserDao.deleteZlxUrl(f_szcu_id);
		return flag;
	}

	public int updateZlxUrl(StatZxbhConfUrlVo statZxbhConfUrlVo){
		int flag = 0;
		flag = zxbhUserDao.updateZlxUrl(statZxbhConfUrlVo);
		return flag;
	}
}